package edu.pdx.cs.cs542s07.minichess;

/**
 * This class is intended to store a transition table for a board. This is actually going
 * to be a static member of the ScoreFactory
 * @author SMITHM
 *
 */
class TransitionTable {

	static final int NUMBER_OF_ENTRIES = 0x400000;
	
	class TTableEntry {
		int hash;
		int score;
		TTableEntry(int hash, int score) {
			this.hash = hash;
			this.score = score;
		}
	}
	
	TTableEntry data[] = new TTableEntry[NUMBER_OF_ENTRIES];
	
	private int getIndexFromHash(int hash) {
		int index = hash % (NUMBER_OF_ENTRIES / 2);
		index += (NUMBER_OF_ENTRIES / 2);
		return index;
	}
	
	void set(Board board, int score) {
		int hash = board.getHash();
		data[getIndexFromHash(hash)] = new TTableEntry(hash, score);
	}
	
	int get(Board board) {
		int hash = board.getHash();
		TTableEntry value = data[getIndexFromHash(hash)];
		if (value != null) {
			return value.score;
		} else {
			return ScoreFactory.IMPOSSIBLE_SCORE;
		}
	}
	
}
